---
title: "Replication material and main analysis"
subtitle: "How terrorist attacks distort public debates: a comparative study of right-wing and Islamist extremism"
date: "`r Sys.Date()`"
author: "Teresa Völker^[WZB Berlin Social Science Center, teresa.voelker@wzb.eu]"
abstract: \singlespacing The success of extremist actors depends on their visibility, resonance, and legitimacy in public debates. Previous research has shown how terrorist attacks attract media attention and influence public opinion and decision-makers. However, we lack a comparative assessment of the extent to which extremist ideologies matter and how they matter. Therefore, this paper compares mass media debates over extreme right and Islamist terrorist attacks. Theoretically, it innovates by linking research on discursive critical junctures and issue-specific discursive opportunity structures, emphasizing the systematic differences between the two ideologies. Empirically, the study is based on an original, large-scale content analysis of mass media debates on all seven fatal attacks in Germany since 2015 (N=9,047). It combines relational quantitative content analysis with frame and network analyses. The results show how the ideology behind terrorist attack shape political response and the framing of the key security threat. Notably, both types of attacks provide favourable conditions for the far right, and political elites play a central role in the diffusion of far-right frames. In contrast, victims and ethnic or religious minorities have little voice in public debates. Overall, the study contributes to a better understanding of the impact of terrorist attacks on Western democracies by emphasizing the impact of ideology and distorted threat perceptions in public debates.
Keywords: far right; terrorism; public debates; right-wing extremism; Islamist extremism; public opinion
output: 
    html_document:
        toc: true
        code_folding: hide
---

# Description

This R script includes the code for the analysis of the paper "How terrorist attacks distort public debates: A comparative study of right-wing and Islamist extremism". It uses the Terrorism Debate Dataset (TDD). This dataset is based on a core-sentence analysis of newspaper articles from the "Süddeutsche Zeitung" and "Die Welt". The focus of the analyses is on mass media debates in Germany after all fatal terrorist attacks since 2015.The time periods cover one week before and two weeks after each attack.

The attacks are the following:

Islamist attacks 

- Berlin 19/12/2016
- Hamburg 28/07/2017
- Dresden 04/10/2020

Extreme right attacks

- München 22/07/2016
- Hanau 19/02/2020
- Halle 09/10/2019
- Lübcke Murder 02/06/2019



```{r setup, include=FALSE}

knitr::opts_chunk$set(echo = TRUE, 
                      message =FALSE, 
                      warning =FALSE
                     )
#change echo=FALSE to hide code for pdf
###load packages

library(plyr)
library(tidyverse)
library(lubridate)
library(writexl)
library(reshape2)
library(kableExtra)
library(ghibli)
library(viridisLite)
library(viridis)
library(readtext)
library(tidyr)
library(dplyr)
library(stringi)
library(stringr)
library(readr)
library(ggrepel)


#renv::activate

```




# Data preparation


```{r load clean data}

# load Terrorist Debate Dataset (TDD)

load("TDD_short.RData")

#rename dataset to all

all <- TDD_short


```





```{r create id for issue_groups}


all <- all%>% 
  dplyr::mutate(subissue_group_id = case_when(subissue_group_eng == "antisemitism"  ~ 1650,
     subissue_group_eng == "asylum"  ~ 1050,                 
     subissue_group_eng == "immigration"  ~ 800,          
      subissue_group_eng == "integration"  ~ 900,          
      subissue_group_eng == "Islam rights"  ~ 600,    
     subissue_group_eng == "Islam symbols"  ~ 400,    
     subissue_group_eng == "Islamist extremism"  ~ 1780,          
     subissue_group_eng == "islamophobia"  ~ 1800,     
     subissue_group_eng == "multiculturalism"  ~ 1100,   
     subissue_group_eng == "national identity"  ~ 1300, 
     subissue_group_eng == "nativism"  ~ 1400,                 
      subissue_group_eng == "racism"  ~ 1200,           
       subissue_group_eng == "radicalisation"  ~ 1820,                 
       subissue_group_eng == "refugees" ~ 1000,
       subissue_group_eng == "right-wing extremism"  ~ 1700,    
  subissue_group_eng == "security politics"  ~ 1900,      
   subissue_group_eng ==    "tradition"  ~ 1500,   
                 TRUE ~ NA_real_))
                

```



## Create variables for cases, period, ideological motive of the event


```{r variable case ideology period}

# Islamist attacks Berlin 19/12/2016 Hamburg Anschlag 28.7.2017 Dresden 04/10/2020

#Right-wing attacks München 22/07/2016; Hanau 19/02/2020 Halle 09/10/2019 Lübcke Murder 02/06/2019

#create variables for cases: one week before and two weeks after attacks

all <- all %>% 
  mutate(case = case_when(date > "2016-07-14" & date < "2016-08-06" ~ "Munich '16",
                          date > "2016-12-11" & date < "2017-01-04" ~ "Berlin '16", 
                          date > "2017-07-20" & date < "2017-08-12" ~ "Hamburg '17",
                          date > "2019-05-24" & date < "2019-06-17" ~ "Luebcke '19", 
                          date > "2019-10-01" & date <"2019-10-24" ~ "Halle '19",
                          date > "2020-02-11" & date <"2020-03-05" ~ "Hanau '20",
                          date > "2020-09-27" & date < "2020-10-19" ~ "Dresden '20",
                          TRUE ~ "other"))


# create variable by ideology
all <- all %>% 
  mutate(ideology = case_when(date > "2016-07-14" & date < "2016-08-06" ~ "right",
                          date > "2016-12-11" & date < "2017-01-04" ~ "is", 
                          date > "2017-07-20" & date < "2017-08-12" ~ "is",
                          date > "2019-05-25" & date < "2019-06-17" ~ "right", 
                          date > "2019-10-01" & date <"2019-10-24" ~ "right",
                          date > "2020-02-11" & date <"2020-03-05" ~ "right",
                          date > "2020-09-27" & date < "2020-10-19" ~ "is",
                          TRUE ~ "other"))


# create variable for period 
all <- all %>% 
  mutate(period = case_when((date > "2016-07-14" & date < "2016-07-23") & case == "Munich '16" ~ "before",
                            (date > "2016-07-22" & date < "2016-07-31") & case == "Munich '16" ~ "week1",
                            (date > "2017-07-20" & date < "2017-07-29") & case == "Hamburg '17" ~ "before",
                            (date > "2017-07-28" & date < "2017-08-06") & case == "Hamburg '17" ~ "week1",
                            (date > "2020-09-27" & date < "2020-10-05") & case == "Dresden '20" ~ "before",
                            (date > "2020-10-04" & date < "2020-10-13") & case == "Dresden '20" ~ "week1",
                            (date > "2019-05-24" & date < "2019-06-03") & case == "Luebcke '19" ~ "before",
                            (date > "2019-06-02" & date < "2019-06-11") & case == "Luebcke '19" ~ "week1",
                            (date > "2019-10-01" & date < "2019-10-10") & case == "Halle '19" ~ "before",
                            (date > "2019-10-10" & date < "2019-10-18") & case == "Halle '19" ~ "week1",
                            (date > "2020-02-11" & date < "2020-02-20") & case == "Hanau '20" ~ "before",
                            (date > "2020-02-19" & date < "2020-02-28") & case == "Hanau '20" ~ "week1",
                            (date > "2016-12-11" & date < "2016-12-20") & case == "Berlin '16" ~ "before",
                            (date > "2016-12-19" & date < "2016-12-27") & case == "Berlin '16" ~ "week1",
                            TRUE & case != "other" ~ "week2"))



```



## Divide cases right-wing & Islamist attacks


```{r split cases}



# Islamist attacks

 
#Berlin 19/12/2016: 12.12.-2.1.2017

berlin <- all %>% filter(date > "2016-12-19" & date < "2017-01-04")

berlin_before <- all %>% filter(date > "2016-12-11" & date < "2016-12-20")

berlin1week <- all %>% filter(date > "2016-12-19" & date < "2016-12-27")

#Hamburg Anschlag 28.7.2017: 21.7-11.8.17

hamburg <- all %>% filter(date > "2017-07-28" & date < "2017-08-12")

hamburg_before <- all %>% filter(date > "2017-07-20" & date < "2017-07-29")

hamburg_1week <- all %>% filter(date > "2017-07-28" & date < "2017-08-06")

#Dresden 04/10/2020

dresden <- all%>% filter(date > "2020-10-04" & date < "2020-10-19")

dresden_before <- all %>% filter(date > "2020-09-27" & date < "2020-10-05")

dresden_week1 <- all%>% filter(date > "2020-10-04" & date < "2020-10-13")


## right attacks

# München 22/07/2016: 15.7.- 5.8.2016
munich<- all %>% filter(date > "2016-07-22" & date < "2016-08-06")

munich_before <-all %>% filter(date > "2016-07-14" & date < "2016-07-23")

munich_week1 <- all %>% filter(date > "2016-07-22" & date < "2016-07-31")


# Lübcke 02/06/2019: 26.5.19- 25.6 (coded till 29.6. to include delayed debate) 

lubcke <- all%>% filter(date > "2019-06-02" & date < "2019-06-17")

lubcke_before <- all %>% filter(date > "2019-05-24" & date < "2019-06-03")

lubcke_1week <- all%>% filter(date > "2019-06-02" & date < "2019-06-11")

## Halle 09/10/2019: 02.10.-23.10.2020  

halle <- all%>% filter(date > "2019-10-09" & date < "2019-10-24")

halle_before <- all %>% filter(date > "2019-10-01" & date < "2019-10-10")

halle_week1 <- all%>% filter(date > "2019-10-09" & date < "2019-10-18")

# Hanau 19/02/2020: 12.2.-4.3.21

hanau <- all %>% filter(date > "2020-02-19" & date < "2020-03-05")

hanau_before <- all%>% filter(date > "2020-02-11" & date < "2020-02-20")

hanau_week1 <- all %>% filter(date > "2020-02-19" & date < "2020-02-28")



# Christchurch 15/03/2019 : 08.03.- 29.03.2019

christ_before <- all %>% filter(date > "2019-03-07" & date < "2019-03-16")

christ <- all %>% filter(date > "2019-03-15" & date < "2019-03-30")

christ_1week <- all %>% filter(date > "2019-03-15" & date < "2019-03-24")

#Buffalo 13.05.2022:	5.5.-27.5 

buffalo<- all %>% filter(date > "2022-05-13" & date < "2022-05-28")


#Oslo 25.6.22: 18.6.-9.7

oslo<- all %>% filter(date > "2022-06-25" & date < "2022-09-08")



```




```{r merge cases}


#Islamist attacks
#Berlin 2016, Hamburg 2017,Dresden 04/10/2020

is_before <- bind_rows (dresden_before, hamburg_before, berlin_before)

is <- bind_rows(berlin,hamburg,dresden)

is_week1 <- bind_rows (dresden_week1, berlin1week,hamburg_1week)


## Merge right attacks 2016-2020

right_before <- bind_rows(munich_before,halle_before,hanau_before,lubcke_before)

# right = all newspaper articles after right attacks
right <- bind_rows (hanau,halle,lubcke, munich)

#  right_1week = one week after all right attacks

right_1week <- bind_rows(halle_week1,hanau_week1,lubcke_1week,munich_week1)

# before & after all national attacks 

all_before <- bind_rows(right_before ,is_before)

all_after <- bind_rows(right,is)

all_1week <- bind_rows (right_1week,is_week1)



## international cases

#Islamist attacks:Paris 16/10/2020, Nizza 29/10/2020,  Wien 02/11/2020

internation_is <- all %>% filter(date > "2020-10-16" & date < "2020-11-17")


# Right attacks: Christchurch 2019, Oslo, Buffalo 2022

international_right <- bind_rows (christ, oslo, buffalo)


# international all

all_internation <- bind_rows (international_right,internation_is)


# all right (national & international)

right_all <- bind_rows (right, international_right)

# all islamist (national & international)


is_all <- bind_rows (is,internation_is)




```





# Descriptive statistics

### Appendix Table B4. Visibility: public reactions after national terrorist attacks

```{r coded articles is right after attacks}


all%>% dplyr::filter(ideology=="right"|ideology =="is") %>% 
  dplyr::count(task_uuid) %>% 
  nrow()%>%kbl(caption="Coded articles before and after national attacks (incl observations)") %>%
  kable_styling()

right%>%  dplyr::count(task_uuid) %>% nrow()%>%kbl(caption="Articles including observations 2 weeks after national right attacks") %>%  kable_styling()


international_right%>% dplyr::count(task_uuid) %>% nrow()%>%kbl(caption="Articles including observations 2 weeks after international right attacks") %>%
  kable_styling()


is%>%  dplyr::count(task_uuid) %>% nrow()%>%kbl(caption="Articles including observations 2 weeks after national Islamist attacks") %>%
  kable_styling()

internation_is%>%  dplyr::count(task_uuid) %>% nrow()%>%kbl(caption="Articles including observations 2 weeks after international Islamist attacks") %>%
  kable_styling()



```




```{r core sentences}

#count & percentage of different types of core-sentences

all%>%filter(!is.na(quality_id)) %>%  dplyr::count(cs_type_string) %>%  mutate(perc=round((perc=n/sum(n)*100),2))%>%kbl(caption="Share Core sentences (All)") %>% kable_styling()


right%>% filter(!is.na(quality_id)) %>%  dplyr::count(cs_type_string) %>%  mutate(perc=round((perc=n/sum(n)*100),2))%>%kbl(caption="Share CS - national extreme right attacks") %>%
  kable_styling()

is%>% filter(!is.na(quality_id)) %>%  dplyr::count(cs_type_string) %>%  mutate(perc=round((perc=n/sum(n)*100),2))%>%kbl(caption="Share CS - national  Islamist attacks") %>% kable_styling()


international_right%>%filter(!is.na(quality_id)) %>%   dplyr::count(cs_type_string) %>%  mutate(perc=round((perc=n/sum(n)*100),2))%>%kbl(caption="Share CS -  international extreme right attacks") %>%
  kable_styling()

internation_is%>% filter(!is.na(quality_id)) %>%  dplyr::count(cs_type_string) %>%  mutate(perc=round((perc=n/sum(n)*100),2))%>%kbl(caption="Share CS -  international Islamist attacks") %>% kable_styling()


# explanation NA core sentence type: Nas are a articles that include no observations / coresentences but a comment during the coding process to re-check the article

```


### Appendix Table B5. Climate in the debate


```{r climate in the debate}

# average positions actor-actor sentences

right %>% dplyr::filter(cs_type_string=="Akteur-Akteur-Satz") %>%
 filter(!is.na(quality_id))%>% summarize(right_averageactorpos=round(mean(quality_id),2)) %>%
  kbl(caption="right attacks-average position a-a-sentences") %>% kable_styling()

is %>% dplyr::filter(cs_type_string=="Akteur-Akteur-Satz") %>%
 filter(!is.na(quality_id))%>% summarize(is_averageactorpos=round(mean(quality_id),2)) %>%
  kbl(caption="is. attacks: average position a-a-sentences") %>% kable_styling()

# average positions actor-actor sentences
##quality_issues : this variable includes the correct positions for the issues (migration, islam, nationalism) 

right %>% dplyr::filter(cs_type_string=="Akteur-Themen-Satz")%>% filter(!is.na(quality_issues))%>% summarize(right_averageissuepos=round(mean(quality_id),2)) %>%
  kbl(caption="right attacks: average position a-i-sentences") %>% kable_styling()

is %>% dplyr::filter(cs_type_string=="Akteur-Themen-Satz") %>%
 filter(!is.na(quality_issues))%>% summarize(is_averageissuepos=round(mean(quality_id),2)) %>%
  kbl(caption="is. attacks: average position a-i-sentences") %>% kable_styling()

```




# Results


# Visibility  

## Visibility terrorist attacks

### Figure 2: Visibility of terrorist attacks in German public debates

```{r context event national}

# terrorist attacks as trigger events overall (including debates 2 weeks after all attacks)
#  count of  (statements) referring to the event -> core-sentence level not article level

# filter national context events (terrorist attacks)
context_all <- all%>% 
  mutate(context= case_when(context_string == "Hanau (19022020)" ~ "Hanau attack 2020",
     context_string == "München (22072016)" ~ "Munich attack 2016",
        context_string == "Messerattacke Hamburg-Barmbek (28072017" ~ "Hamburg attack 2017",
                                                            context_string == "Halle (09102019)" ~ "Halle attack 2019",
                                 context_string == "Lübcke Mord (01062019)" ~ "Lübcke murder 2019",
                                      context_string == "Berlin (19122016)" ~ "Berlin attack 2016",
                                  context_string == "Dresden (04102020)" ~ "Dresden attack 2020")) %>%
  dplyr::count(context) %>% 
  filter(!is.na(context)) %>% mutate(perc=round((n/sum(n)*100),2)) %>% 
   top_n(9)%>%
  arrange(desc(n))



plot_context_nat <- ggplot(context_all, aes(x=reorder(context, -n), y=n))+
          geom_bar(stat = "identity", aes(fill=factor(context))) +
scale_fill_manual(breaks = c("Berlin attack 2016","Halle attack 2019", "Hanau attack 2020", "Lübcke murder 2019","Dresden attack 2020", "Hamburg attack 2017", "Munich attack 2016"),
                        values=c("#FF9999","#33CCCC","#33CCCC","#33CCCC","#FF9999", "#FF9999","#33CCCC"))+
  ylab("public sentences (count)") + 
  xlab("Trigger event") +
  theme_light() + 
  theme(axis.text.x = element_text(angle=65, hjust=1))+
  #ggtitle("Public attention for terrorism in Germany") +
labs(fill = "event") +
 theme(legend.position="none")

plot_context_nat 

#ggsave(filename="contextevent_national.png", plot=last_plot(), device="png", units="cm", width=20, height=13, dpi=300)

#ggsave(filename="contextevent_national.jpeg", plot=last_plot(), device="jpeg", units="cm", width=20, height=13, dpi=300)



```




### Appendix Figure B1. Development public visibility terrorist attacks two weeks after the attacks (examples)


**a)	after right-wing attacks in Halle 2010 and Hanau 2020**


```{r context over time halle-eng}

#set system to english to get english dates

Sys.setlocale(category = "LC_ALL", locale = "english")


context_date_hal <- halle %>% 
  dplyr::filter(!is.na(context_string_eng))%>% 
  dplyr::mutate(count = 1) %>% 
  dplyr::group_by(date,context_string_eng) %>% 
  dplyr::summarise(count_context = sum(count)) 
 

# fill incomplete times series, create new df with complete dates and merge afterwards

full_halle <- data.frame(date=seq(ymd('2019-10-09'),ymd('2019-10-23'), by = '1 day'))


context_date_hal2 <- left_join(full_halle, context_date_hal, by="date")



##complete = add missing strings
library(tidyr)
context_date_hal2 <- context_date_hal2 %>% 
 tidyr:: complete(context_string_eng, nesting(date)) %>% 
    dplyr::filter(!is.na(context_string_eng)) %>% ungroup()
    
context_date_hal2$count_context[is.na(context_date_hal2$count_context)] <- 0

context_date_hal2 <- data.frame(context_date_hal2)

# plot
plot_context_date_hal <- ggplot(context_date_hal2, aes(x=date, y=count_context)) + 
    geom_area(alpha=0.6 , size=.5, position='stack', colour="white", aes(fill=factor(context_string_eng))) +
      scale_fill_brewer(palette="Set2")+
    ylab("count context event") +
    xlab("") +
   # ggtitle("Effect of trigger event: Halle 2o19") +
    labs(fill = "trigger event") + 
    theme(axis.text.x=element_text(angle=45, vjust=.5), 
          axis.title.y = element_text(size=12)) 

  

# add attack in plot

plot_context_date_hal<- plot_context_date_hal +
 coord_cartesian(ylim = c(1, 200))+
  geom_vline(xintercept=as.numeric(ymd("2019-10-09")), linetype="dashed", color="grey")+
    geom_text(aes(x=ymd("2019-10-09"), label="Halle attack 09/10/2019", y=100), colour="black", angle=90, text=element_text(size=12)) + 
  theme_light()+
   scale_x_date(date_breaks = "5 day", date_labels = "%b %d")
  
plot_context_date_hal <- plot_context_date_hal + theme(legend.position = "bottom") 


plot_context_date_hal

#ggsave(filename="context_overtime_halle.jpeg", plot=plot_context_date_hal, device="jpeg", units="cm", width=20, height=13, dpi=300)

#ggsave(filename="context_overtime_halle.png", plot=plot_context_date_hal, device="png", units="cm", width=20, height=13, dpi=300)

```




```{r context over time hanau - engl}

#set system to english to get english dates

Sys.setlocale(category = "LC_ALL", locale = "english")

# zeitverlauf kürzen, in dem genug codiert wurde

#hanau_n <- hanau %>% filter(date < "2020-03-09")

context_date_ha <- hanau %>% 
  dplyr::filter(!is.na(context_string_eng))%>% 
  dplyr::mutate(count = 1) %>% 
  dplyr::group_by(date,context_string_eng) %>% 
  dplyr::summarise(count_context = sum(count))
 

#fill incomplete times series, create new df with complete dates and merge afterwards


# adapt date

full_ha <- data.frame(date=seq(ymd('2020-02-19'),ymd('2020-03-05'), by = '1 day'))

context_date_ha2 <- left_join(full_ha, context_date_ha, by="date")


##complete = add missing strings
library(tidyr)
context_date_ha2 <- context_date_ha2 %>% 
 tidyr:: complete(context_string_eng, nesting(date)) %>% 
    dplyr::filter(!is.na(context_string_eng)) %>% ungroup()
    
context_date_ha2$count_context[is.na(context_date_ha2$count_context)] <- 0

context_date_ha2 <- data.frame(context_date_ha2)

plot_context_date_ha <- ggplot(context_date_ha2, aes(x=date, y=count_context)) + 
    geom_area(alpha=0.6 , size=.5, position='stack', colour="white", aes(fill=factor(context_string_eng))) +
 scale_fill_brewer(palette="Set2")+
    ylab("count context event") +
    xlab("") +
   # ggtitle("Effect of trigger event: Hanau 2o2o") +
    labs(fill = "trigger event") + 
    theme(axis.text.x=element_text(angle=45, vjust=.5), 
          axis.title.y = element_text(size=12)) 


# add attacks to graph

plot_context_date_ha <- plot_context_date_ha  +
 coord_cartesian(ylim = c(1, 200))+
  geom_vline(xintercept=as.numeric(ymd("2020-02-19")), linetype="dashed", color="grey")+
    geom_text(aes(x=ymd("2020-02-19"), label="Hanau attack 19/02/2020", y=100), colour="black", angle=90, text=element_text(size=12)) + 
  theme_light()+ 
    scale_x_date(date_breaks = "5 day", date_labels = "%b %d") +
theme(legend.position = "bottom") 



plot_context_date_ha

#ggsave(filename="context_overtime_hanau.jpeg", plot=plot_context_date_ha, device="jpeg", units="cm", width=20, height=13, dpi=300)

```


**b)	after Islamist attacks in Berlin 2016 and Dresden 2020**


```{r context over time berlin}

#set system to english to get english dates

Sys.setlocale(category = "LC_ALL", locale = "english")


context_date_b <- berlin %>% 
  dplyr::filter(!is.na(context_string_eng))%>% 
  dplyr::mutate(count = 1) %>% 
  dplyr::group_by(date,context_string_eng) %>% 
  dplyr::summarise(count_context = sum(count)) 
 

#fill incomplete times series, create new df with complete dates and merge afterwards

 #date: "Berlin '19.12.16", 

full_b <- data.frame(date=seq(ymd('2016-12-19'),ymd('2017-01-04'), by = '1 day'))


context_date_b <- left_join(full_b, context_date_b, by="date")



##complete= add context_string
library(tidyr)
context_date_b <- context_date_b %>% 
 tidyr:: complete(context_string_eng, nesting(date)) %>% 
    dplyr::filter(!is.na(context_string_eng)) %>% ungroup()
    
context_date_b$count_context[is.na(context_date_b$count_context)] <- 0

context_date_b <- data.frame(context_date_b)

plot_context_date_b <- ggplot(context_date_b, aes(x=date, y=count_context)) + 
    geom_area(alpha=0.6 , size=.5, position='stack', colour="white", aes(fill=factor(context_string_eng))) +
      scale_fill_brewer(palette="Set2")+   scale_fill_brewer(palette="Set3")+
  ylab("count context event") +
    xlab("") +
   # ggtitle("Effect of trigger event: Berlin") +
    labs(fill = "trigger event") + 
    theme(axis.text.x=element_text(angle=45, vjust=.5), 
          axis.title.y = element_text(size=12)) 

  

# plot

plot_context_date_b<- plot_context_date_b +
 coord_cartesian(ylim = c(1, 200))+
  geom_vline(xintercept=as.numeric(ymd("2016-12-19")), linetype="dashed", color="grey")+
    geom_text(aes(x=ymd("2016-12-19"), label="Berlin 19/12/2016", y=100), colour="black", angle=90, text=element_text(size=12)) + 
  theme_light()+
   scale_x_date(date_breaks = "5 day", date_labels = "%b %d")
  
plot_context_date_b <- plot_context_date_b + theme(legend.position = "bottom") 


plot_context_date_b


#ggsave(filename="context_overtime_berlin.jpeg", plot=plot_context_date_b, device="jpeg", units="cm", width=20, height=13, dpi=300)

```



```{r context over time is engl}

#set system to english to get english dates

Sys.setlocale(category = "LC_ALL", locale = "english")


context_date <- is_all %>% 
  dplyr::filter(!is.na(context_string_eng))%>% 
  dplyr::mutate(count = 1) %>% 
  dplyr::group_by(date,context_string_eng) %>% 
  dplyr::summarise(count_context = sum(count)) 
 

#fill incomplete times series, create new df with complete dates and merge afterwards


full2 <- data.frame(date=seq(ymd('2020-10-03'),ymd('2020-11-16'), by = '1 day'))

context_date2 <- left_join(full2, context_date, by="date")



##complete = add missing context string
library(tidyr)
context_date2 <- context_date2  %>% 
 tidyr:: complete(context_string_eng, nesting(date)) %>% 
    dplyr::filter(!is.na(context_string_eng)) %>% ungroup()
    
context_date2$count_context[is.na(context_date2$count_context)] <- 0

context_date2 <- data.frame(context_date2)


# plot

#color:Spectral or Set3

plot_context_date <- ggplot(context_date2, aes(x=date, y=count_context)) + 
    geom_area(alpha=0.6 , size=.5, position='stack', colour="white", aes(fill=factor(context_string_eng))) +
   scale_fill_brewer(palette="Set3")+
     ylab("count context event") +
    xlab("") +
    #ggtitle("Effect of trigger events: Islamist attacks 2o2o") +
    labs(fill = "event") + 
    theme_light()+
    theme(axis.text.x=element_text(angle=45, vjust=.5), 
          axis.title.y = element_text(size=11)) 

  
# add attacks in graph
#Paris:2020-10-16, Dresden: 2020-10-04, Nizza: 2020-10-29, Wien: 2020-11-02

plot_context_date <- plot_context_date +
 coord_cartesian(ylim = c(1, 150))+
geom_vline(xintercept=as.numeric(ymd("2020-11-02")))+
    geom_text(aes(x=ymd("2020-11-03"), label="Vienna attack 2/11", y=80), colour="black", angle=90, text=element_text(size=9)) +
 geom_vline(xintercept=as.numeric(ymd("2020-10-29"))) +
   geom_text(aes(x=ymd("2020-10-30"), label="Nice attack 29/10", y=80), colour="black", angle=90, text=element_text(size=9)) +
  geom_vline(xintercept=as.numeric(ymd("2020-10-16")))+
  geom_text(aes(x=ymd("2020-10-17"), label="Paris attack 16/10", y=80), colour="black", angle=90, text=element_text(size=9)) +
  geom_vline(xintercept=as.numeric(ymd("2020-10-04")))+
  geom_text(aes(x=ymd("2020-10-05"), label="Dresden attack 04/10", y=80), colour="black", angle=90, text=element_text(size=9)) +
  theme(legend.position = "bottom") 

plot_context_date


#ggsave(filename="context_overtime_is.jpeg", plot=last_plot(), device="jpeg", units="cm", width=22, height=13, dpi=300)

```




**Prepare data for actor analyses**


```{r actor type distribution right is}

## merge subject & object

#select variables 
actor_type_r <- select(right,c("subject1_eng", "object1_eng"))  

## make dateset longer
actor_type_r <-pivot_longer(actor_type_r, cols = c("subject1_eng", "object1_eng"))


#rename value variable 
actor_type_r <- dplyr::rename(actor_type_r,actor=value)

#remove missings
actor_type_r<- actor_type_r %>%
  filter(!is.na(actor))


## is
#merge subject & object

#select variables 
actor_type_i <- select(is,c("subject1_eng", "object1_eng"))  

## make dateset longer
actor_type_i <-pivot_longer(actor_type_i, cols = c("subject1_eng", "object1_eng"))


#rename value variable 
actor_type_i <- dplyr::rename(actor_type_i,actor=value)

#remove missings
actor_type_i<- actor_type_i %>%
  filter(!is.na(actor))


#count actor types is & right

actor_type_r_count <- actor_type_r%>% 
   filter(!is.na(actor)) %>%
  group_by(actor)%>% 
  mutate(count_right=1) %>% 
  dplyr::summarise(count_right = sum(count_right))%>% 
  mutate (pct_right_attacks=count_right/sum(count_right)*100)%>% 
  arrange(desc(count_right))

actor_type_i_count <- actor_type_i %>% 
   filter(!is.na(actor)) %>%
  group_by(actor)%>% 
  mutate(count_islamist=1) %>% 
  dplyr::summarise(count_islamist = sum(count_islamist))%>% 
  mutate (pct_islamist_attacks=count_islamist/sum(count_islamist)*100)%>% 
  arrange(desc(count_islamist))
  
##top actors is & right
actor_type_r_top<- top_n(actor_type_r_count,14)

actor_type_i_top <- top_n(actor_type_i_count,14)

```





```{r count subject and object is right}


actor_subject <- all_after%>% 
  group_by(subject1_eng)%>% 
   filter(!is.na(subject1_eng)) %>% 
  mutate(count=1) %>% 
  dplyr::summarise(count = sum(count))%>% 
  mutate (pct=round((count/sum(count)*100),2)) %>% 
arrange(desc(count))

actor_subject_i <- is%>% 
  group_by(subject1_eng)%>% 
   filter(!is.na(subject1_eng)) %>% 
  mutate(count=1) %>% 
  dplyr::summarise(count_is = sum(count))%>% 
  mutate (pct_islamist_attacks=round((count_is/sum(count_is)*100),2)) %>% 
arrange(desc(count_is))

actor_subject_r <- right%>% 
  group_by(subject1_eng)%>% 
   filter(!is.na(subject1_eng)) %>% 
  mutate(count=1) %>% 
  dplyr::summarise(count_right = sum(count))%>% 
  mutate (pct_right_attacks=round((count_right/sum(count_right)*100),2)) %>% 
arrange(desc(count_right))

#export excel

#write_xlsx(actor_subject_r, "actor_subject_r.xlsx")
#write_xlsx(actor_subject_i, "actor_subject_i.xlsx")

#all
actor_object <- all_after%>% 
  group_by(object1_eng)%>% 
   filter(!is.na(object1_eng)) %>% 
  mutate(count=1) %>% 
  dplyr::summarise(count = sum(count))%>% 
  mutate (pct=round((count/sum(count)*100),2)) %>% 
arrange(desc(count))

#right
actor_object_r <- right %>% 
  group_by(object1_eng)%>% 
   filter(!is.na(object1_eng)) %>% 
  mutate(count=1) %>% 
  dplyr::summarise(count_right = sum(count))%>% 
  mutate (pct_right_attacks=round((count_right/sum(count_right)*100),2)) %>% 
arrange(desc(count_right))


#is
actor_object_i <- is%>% 
  group_by(object1_eng)%>% 
   filter(!is.na(object1_eng)) %>% 
  mutate(count=1) %>% 
  dplyr::summarise(count_is = sum(count))%>% 
  mutate (pct_islamist_attacks=round((count_is/sum(count_is)*100),2)) %>% 
arrange(desc(count_is))

#export excel
#write_xlsx(actor_object_i, "actor_object_i.xlsx")
#write_xlsx(actor_object_r, "actor_object_r.xlsx")

```


## Appendix: Figure B2. Resonance actor types after Islamist and right-wing attacks


```{r merge visualize all subjects is and right}

#merge subjects after is and right attacks for overview & visualization
   
actor_subject_isright <- left_join(actor_subject_i, actor_subject_r , by="subject1_eng")


#write_xlsx(actor_subject_isright, "actor_subject_isright.xlsx")


#use merged data

## make dateset longer
actor_type_all2  <-pivot_longer(actor_subject_isright, cols = c("pct_right_attacks", "pct_islamist_attacks"))

#rename 
actor_type_all2 <-dplyr::rename(actor_type_all2 ,ideology_attack=name)



#visualize subject actors islamist & right-wing
# stack=on top of each other ; dodge =next to each other
# scale y-lab:  ylim(0,100)+

plot_subjectsall<- ggplot(actor_type_all2, aes(x=reorder(subject1_eng, -value), y=value, fill=ideology_attack)) + 
  geom_bar(stat = 'identity', position = 'dodge') +
  ylab("share sentences in %") + 
  xlab("Actor types") +
theme_light()+
   theme(axis.text.x = element_text(angle=65, hjust=1))+
  scale_fill_manual(values = c("pct_right_attacks" = "#33CCCC", "pct_islamist_attacks" = "#FF9999"),
                    labels = c("pct_right_attacks" = "extreme right attack", "pct_islamist_attacks" = "Islamist attack"))  # Mapping of values to labels

# Rename the variable in the legend
plot_subjectsall<- plot_subjectsall + labs(fill = "ideology")


plot_subjectsall

#ggsave(filename="plot_subjectsall.jpeg", plot=plot_subjectsall, device="jpeg", units="cm", width=20, height=13, dpi=300)


```



### Figure 3: Public visibility of extremists versus victims

```{r visibility extremists vs victims}

# filter victims and extremists as subject actor

subject_vic<- actor_subject_isright %>%   
  mutate(actor = case_when(   
                    subject1_eng== "extreme right actors" ~ "Right-wing extremists",
    subject1_eng== "Islamist actors" ~ "Islamist extremists",
    subject1_eng== "ethnic minority groups" ~ "ethnic/religious minorities",
      subject1_eng== "victims" ~ "victims general",
 subject1_eng== "scientists and experts" ~ "other",
                              subject1_eng== "individuals" ~ "other",
                           subject1_eng== "supranational organisations" ~ "other",
                            subject1_eng== "left groups" ~ "other",
                       subject1_eng== "sport and cultural organisations" ~ "other",
                    subject1_eng== "muslim actors" ~ "Muslim actors",
                 subject1_eng== "ethnic minority groups" ~ "other minorities",
            subject1_eng== "Jewish actors" ~ "Jewish actors", ))


#filter Nas and other actors
subject_vic <-  subject_vic%>% filter(!is.na(actor))  


subject_vic <-  subject_vic%>% filter(!actor=="other") 


#subject_central <- subject_central %>% filter(!(actor=="other"))


## make dateset longer
actor_type_all4  <-pivot_longer(subject_vic , cols = c("pct_right_attacks", "pct_islamist_attacks"))

#rename 
actor_type_all4 <-dplyr::rename(actor_type_all4 ,ideology_attack=name)



#plat subject actors islamist & right-wing
# stack=on top of each other ; dodge =next to each other
# scale y-lab:  ylim(0,100)+

plot_subjects4 <- ggplot(actor_type_all4, aes(x = reorder(actor, -value), y = value, fill = ideology_attack)) + 
  geom_bar(stat = 'identity', position = 'dodge') +
  ylab("share sentences in %") + 
  xlab("Actor types") +
  theme_light() +
  theme(axis.text.x = element_text(angle = 65, hjust = 1)) +
  scale_fill_manual(values = c("pct_right_attacks" = "#33CCCC", "pct_islamist_attacks" = "#FF9999"),
                    labels = c("pct_right_attacks" = "extreme right attack", "pct_islamist_attacks" = "Islamist attack"))  # Mapping of values to labels

# Rename the variable in the legend
plot_subjects4 <- plot_subjects4 + labs(fill = "ideology")

plot_subjects4


#ggsave(filename="resonance_victims.jpeg", plot=last_plot(), device="jpeg", units="cm", width=20, height=13, dpi=300)




```






```{r names extremists versus victims}

# calculate total & share names; after right attack = N= 2808, after islamist attacks=N=1638

names_victims_right <- right%>% 
     filter(subject1_eng=="victims")%>%
  group_by(subject3_string)%>% 
  mutate(count=1) %>% 
  dplyr::summarise(count = sum(count))%>% 
  mutate(victim_right=count/2808*100)%>% 
arrange(desc(count))

names_victims_is <- is%>% 
     filter(subject1_eng=="victims")%>%
  group_by(subject3_string)%>% 
  mutate(count=1) %>% 
  dplyr::summarise(count = sum(count))%>% 
  mutate(victim_is=count/1638*100)%>% 
arrange(desc(count))

names_extremist_right <- right%>% 
     filter(subject1_eng=="extreme right actors")%>%
  group_by(subject3_string)%>% 
  mutate(count=1) %>% 
  dplyr::summarise(count = sum(count))%>% 
  mutate(ex_right=count/2808*100)%>% 
arrange(desc(count))

names_extremist_is <- is %>% 
     filter(subject1_eng=="Islamist actors")%>%
  group_by(subject3_string)%>% 
  mutate(count=1) %>% 
  dplyr::summarise(count = sum(count))%>% 
  mutate(ex_is=count/1638*100)%>% 
arrange(desc(count))

names_1 <- full_join(names_extremist_is, names_extremist_right,by="subject3_string")

names_2 <- full_join(names_victims_is, names_victims_right, by="subject3_string")


names_extremist_victims <- full_join(names_1,names_2, by="subject3_string")

names_extremist_victims <- names_extremist_victims %>% select(-count.x.x,-count.y.y, -count.y.x,-count.x.y)  %>%  filter(!is.na(subject3_string))


#export victim & extremist share of names 
#write_xlsx(names_extremist_victims, "names_extremist_victims.xlsx")


```






```{r mention terrorists }

##subject: count terrorist mentions

# note: this variable only includes sentences that directly referred and named the perpetrator as a terrorist --> sentences describing him as an extremist are not included


terrorist_subject <- all_after %>% dplyr::count(terrorist) %>%  mutate(perc=round((n/sum(n)*100),2)) %>%arrange(desc(n))

## object


terrorist_object <- all_after %>% dplyr::count(terrorist_o) %>%  mutate(perc=round((n/sum(n)*100),2)) %>%arrange(desc(n))


```



```{r visualize all objects is and right}

#merge subjects after is and right attacks for overview & visualization
actor_object_isright <- left_join(actor_object_i, actor_object_r , by="object1_eng")


#write_xlsx(actor_object_isright, "actor_object_isright.xlsx")

# summarize actor types - other & ethnic/religious minorities (including victims)
#subsume police under executive |actor== "police and security authorities"

#use merged data

## make dataset longer
actor_type_all4  <-pivot_longer(actor_object_isright, cols = c("pct_right_attacks", "pct_islamist_attacks"))

#rename 
actor_type_all4 <-dplyr::rename(actor_type_all4 ,ideology_attack=name)



#visualize subject actors Islamist & right-wing
# stack=on top of each other ; dodge =next to each other
# scale y-lab:  ylim(0,100)+

plot_objectsall<- ggplot(actor_type_all4, aes(x=reorder(object1_eng, -value), y=value, fill=ideology_attack)) + 
  geom_bar(stat = 'identity', position = 'dodge', alpha = 0.6) +
  ylab("share sentences in %") + 
  xlab("Actor types") +
  theme_minimal() + 
   theme(axis.text.x = element_text(angle=65, hjust=1))+
  ggtitle("Resonance objects in the context of terrorist attacks")

plot_objectsall


```





# Resonance 


### Table 2: Resonance: share of political parties as subject and object in political debates after terrorist attacks, percentages

**Part 1: share of political parties as subject in political debates after terrorist attacks, percentages**

```{r party distribution as subjects after attacks}



#create table
parties_right <- right%>% 
  group_by(subject_2_string) %>% 
   filter(!is.na(subject_2_string)) %>% 
  filter(subject_2_string=="CDU" | subject_2_string=="AfD" |subject_2_string=="SPD" | subject_2_string=="FDP" | subject_2_string=="Die Linke" |subject_2_string=="CSU" | subject_2_string=="Bündnis 90/Die Grünen")%>% 
  mutate(count=1) %>% 
  dplyr::summarise(count = sum(count))%>% 
  mutate (pct_right=round((count/sum(count)*100),2)) %>% 
arrange(desc(count)) 



# party distribution after is attacks
parties_is <- is%>% 
  group_by(subject_2_string) %>% 
   filter(!is.na(subject_2_string)) %>% 
  filter(subject_2_string=="CDU" | subject_2_string=="AfD" |subject_2_string=="SPD" | subject_2_string=="FDP" | subject_2_string=="Die Linke" |subject_2_string=="CSU" | subject_2_string=="Bündnis 90/Die Grünen")%>% 
  mutate(count=1) %>% 
  dplyr::summarise(count = sum(count))%>% 
  mutate (pct_is=round((count/sum(count)*100),2)) %>% 
arrange(desc(count))

# merge for export

parties_as_subjects <- left_join(parties_right,parties_is, by="subject_2_string")

#write_xlsx(parties_as_subjects, "parties_as_subjects.xlsx")

parties_as_subjects

```


**Part 2: share of political parties as subject in political debates after terrorist attacks, percentages**

```{r party distribution as objects after attacks}



#create table

parties_right_o <- right%>% 
  group_by(object_2_string) %>% 
   filter(!is.na(object_2_string)) %>% 
  filter(object_2_string=="CDU" | object_2_string=="AfD" |object_2_string=="SPD" |object_2_string=="FDP" | object_2_string=="Die Linke" |object_2_string=="CSU" | object_2_string=="Bündnis 90/Die Grünen")%>% 
  mutate(count=1) %>% 
  dplyr::summarise(count = sum(count))%>% 
  mutate (pct_right=round((count/sum(count)*100),2)) %>% 
arrange(desc(count)) 

# party distribution after is attacks
parties_is_o <- is%>% 
  group_by(object_2_string) %>% 
   filter(!is.na(object_2_string)) %>% 
  filter(object_2_string=="CDU" |object_2_string=="AfD" |object_2_string=="SPD" | object_2_string=="FDP" | object_2_string=="Die Linke" |object_2_string=="CSU" | object_2_string=="Bündnis 90/Die Grünen")%>% 
  mutate(count=1) %>% 
  dplyr::summarise(count = sum(count))%>% 
  mutate (pct_is=round((count/sum(count)*100),2)) %>% 
arrange(desc(count))

parties_as_objects <- left_join(parties_right_o,parties_is_o, by="object_2_string")

#export
#write_xlsx(parties_as_objects, "parties_as_objects.xlsx")

parties_as_objects


```






# Resonance Issues


```{r issues}

# note: radicalisation = issue that covers security politics and fight against radicalization
# all issues (after right &islamist attacks)

all%>% dplyr::count(issue_string_eng) %>%
  filter(!is.na(issue_string_eng)) %>%
 mutate(perc=round((n/sum(n)*100),2))%>% arrange(desc(n))%>% 
kbl(caption="Issue distribution (all attacks)") %>%
  kable_styling()


right%>% dplyr::count(issue_string_eng) %>%
  filter(!is.na(issue_string_eng)) %>%
 mutate(perc=round((n/sum(n)*100),2))%>% arrange(desc(n))%>% 
kbl(caption="Issue distribution after extreme right attacks") %>%
  kable_styling()

is%>% dplyr::count(issue_string_eng) %>%
  filter(!is.na(issue_string_eng)) %>%
 mutate(perc=round((n/sum(n)*100),2))%>% arrange(desc(n))%>% 
kbl(caption="Issue distribution after Islamist attacks") %>%
  kable_styling()




```







## Figure 4: Resonance of central sub-issues, share of statements 


```{r issue groups is and right}


#count issues, group by ideology, filter top issues
subissue_comp <- all_after%>% 
  dplyr::filter(!is.na(subissue_group_eng))%>% 
  mutate(count = 1) %>% 
  group_by(ideology,subissue_group_eng) %>% 
  dplyr::summarise(count_issue = sum(count))%>%
  mutate (pct=count_issue/sum(count_issue)*100)%>%
   top_n(13) %>%
arrange(desc(count_issue))

#plot

plot_subissue_comp<- ggplot(subissue_comp, aes(x=reorder(subissue_group_eng, -pct), y=pct, fill=ideology)) + 
  geom_bar(stat = 'identity', position = 'dodge') +
  ylab("share sentences in %") + 
  xlab("Sub-Issues") +
    theme_light()+
   theme(axis.text.x = element_text(angle=65, hjust=1))+
  scale_fill_manual(values = c("right" = "#33CCCC", "is" = "#FF9999"),
                    labels = c("right" = "extreme right attack", "is" = "Islamist attack"))  

 plot_subissue_comp 



#ggsave(filename="plot_subissue_comp.jpeg", plot=plot_subissue_comp, device="jpeg", units="cm", width=20, height=13, dpi=300)

```


### Table B9. Resonance sub-issues after Islamist and right-wing attacks 


```{r subissues after attacks}



#is
issuegroups_is <- is%>% dplyr::count(subissue_group_eng) %>%
  filter(!is.na(subissue_group_eng)) %>%
 mutate(perc_is=round((n/sum(n)*100),2))

issuegroups_is%>% 
kbl(caption="Issue Groups: Islamist attacks") %>%
  kable_styling()

# right 

issuegroups_right <- right%>% dplyr::count(subissue_group_eng) %>%
  filter(!is.na(subissue_group_eng)) %>%
 mutate(perc_right=round((n/sum(n)*100),2))

issuegroups_right%>% 
kbl(caption="Issue Groups: Right-wing attacks") %>%
  kable_styling()



issuegroups_comp <- full_join(issuegroups_right, issuegroups_is, by="subissue_group_eng")

#export

#write_xlsx(issuegroups_comp, "issuegroups_comparison_isright.xlsx")

```




```{r subissues before attacks}



#is
issuegroups_before_is <- is_before%>% dplyr::count(subissue_group_eng) %>%
  filter(!is.na(subissue_group_eng)) %>%
 mutate(perc_is=round((n/sum(n)*100),2))

issuegroups_before_is%>% 
kbl(caption="Issue Groups:before Islamist attacks") %>%
  kable_styling()

# right 

issuegroups_before_right <- right_before%>% dplyr::count(subissue_group_eng) %>%
  filter(!is.na(subissue_group_eng)) %>%
 mutate(perc_right=round((n/sum(n)*100),2))

issuegroups_before_right%>% 
kbl(caption="Issue Groups: before Right-wing attacks") %>%
  kable_styling()



issuegroups_before_comp <- full_join(issuegroups_before_right, issuegroups_before_is, by="subissue_group_eng")
#export


#write_xlsx(issuegroups_before_comp, "issuegroups_before_comparison_isright.xlsx")


```



### Appendix Table B12. Political level for regulation


```{r  level internationalisation}


# regulation level of demands


level_right <- right%>%
dplyr::count(level_string) %>%
  filter(!is.na(level_string)) %>%
 mutate(perc_right=round((n/sum(n)*100),2))

level_right%>% 
kbl(caption="Level: after right attacks") %>%
  kable_styling()

level_is <- is%>%
dplyr::count(level_string) %>%
  filter(!is.na(level_string)) %>%
 mutate(perc_is=round((n/sum(n)*100),2))

level_is %>% 
kbl(caption="Level: after islamist attacks") %>%
  kable_styling()

# export

level <- left_join(level_is, level_right, by="level_string")

#write_xlsx(level, "regulation_level.xlsx")





```




# Legitimacy 

### Table 3: Legitimacy shift of actors and issues

**Table 3 Part I: Legitimacy shift of issues***

```{r legitimacy issues}

 # mean position before and after events
#quality issues = position variable on issue level

##is

#before
legitim_is_bef <- is_before %>%
  dplyr:: group_by(issue_string_eng)%>% 
    filter(cs_type_id==1)%>%
  filter(!is.na(issue_string_eng))%>% filter(!is.na(quality_issues))%>%  dplyr::summarize(before_is=mean(quality_issues))


 #after
 
 legitim_is <- is_week1 %>%
 dplyr:: group_by(issue_string_eng)%>% 
    filter(cs_type_id==1)%>%
  filter(!is.na(issue_string_eng))%>% filter(!is.na(quality_issues))%>%  dplyr::summarize(after_is=mean(quality_issues))


 ## right
 
 #before
legitim_right_bef <-  right_before %>%
  dplyr:: group_by(issue_string_eng)%>% 
    filter(cs_type_id==1)%>%
  filter(!is.na(issue_string_eng))%>% filter(!is.na(quality_issues))%>%  dplyr::summarize(before_right=mean(quality_issues))

  
 #after
legitim_right <-right_1week %>%
 dplyr:: group_by(issue_string_eng)%>% 
    filter(cs_type_id==1)%>%
  filter(!is.na(issue_string_eng))%>% filter(!is.na(quality_issues))%>%  dplyr::summarize(after_right=mean(quality_issues))


## merge legitimacy before and after attacks 

legitim_shift_right <- left_join(legitim_right_bef, legitim_right, by="issue_string_eng")

legitim_shift_is <- left_join(legitim_is_bef, legitim_is, by="issue_string_eng")


## calculate shifts (after-before average position)

legitim_shift_is <-legitim_shift_is %>% dplyr::mutate(shift_is=after_is-before_is)

legitim_shift_right <- legitim_shift_right %>% dplyr::mutate(shift_right=after_right-before_right)


#final table: merge legitimacy shifts after Islamist and right attacks

legitim_shift <- left_join(legitim_shift_is,legitim_shift_right,by="issue_string_eng")


#export excel
#write_xlsx(legitim_shift, "legitimacy_shift_issues_isright.xlsx")



legitim_shift %>%
  kbl(caption="Legitimacy shift issues after Islamist and extreme right attacks") %>%
  kable_styling()


```


**Table 3 Part 2: Legitimacy shift of actors**

```{r legitimacy actors}


 # mean position before and after events
#quality id = position variable on actor level
#filter only a-a-sentences - filter(cs_type_id==2)%>%
#is

#before
legitim_actor_is_bef<- is_before %>%
  dplyr:: group_by(object1_eng)%>% 
    filter(cs_type_id==2)%>%
  filter(!is.na(object1_eng))%>% filter(!is.na(quality_id))%>%  dplyr::summarize(before_is=mean(quality_id))

#after
 
 legitim_actor_is <- is_week1 %>%
  dplyr:: group_by(object1_eng)%>% 
    filter(cs_type_id==2)%>%
  filter(!is.na(object1_eng))%>% filter(!is.na(quality_id))%>%  dplyr::summarize(after_is=mean(quality_id))

 
 ## right
 
 #before
 legitim_actor_right_bef <- right_before %>%
  dplyr:: group_by(object1_eng)%>% 
    filter(cs_type_id==2)%>%
  filter(!is.na(object1_eng))%>% filter(!is.na(quality_id))%>%  dplyr::summarize(before_right=mean(quality_id))
  
  #after
 
legitim_actor_right <- right_1week %>%
 filter(!is.na(context_string))%>%
  dplyr:: group_by(object1_eng)%>% 
    filter(cs_type_id==2)%>%
  filter(!is.na(object1_eng))%>% filter(!is.na(quality_id))%>%  dplyr::summarize(after_right=mean(quality_id))

   
## merge legitimacy before and after attacks 

legitim_actor_shift_right <- full_join(legitim_actor_right_bef, legitim_actor_right, by="object1_eng")

legitim_actor_shift_is <- full_join(legitim_actor_is_bef, legitim_actor_is, by="object1_eng")


## calculate shifts (after-before average position)

legitim_actor_shift_is <-legitim_actor_shift_is %>% dplyr::mutate(shift_is=after_is-before_is)

legitim_actor_shift_right <- legitim_actor_shift_right %>% dplyr::mutate(shift_right=after_right-before_right)


#final table: merge legitimacy shifts after Islamist and right attacks

legitim_actor_shift <- full_join(legitim_actor_shift_is,legitim_actor_shift_right,by="object1_eng")


#write_xlsx(legitim_actor_shift, "legitim_actor_shift.xlsx")

legitim_actor_shift %>%
  kbl(caption="Legitimacy shift actors after Islamist and extreme right attacks") %>%
  kable_styling()

```



### Table B13. Legitimacy shift of actors and issues (change of average position in statements from one week before to 1 week after attacks)

**Table B13. Part I: Legitimacy shift of issues***

```{r legitimacy issues numbers of observations}

# add number of core-sentences (N) to legitimacy table


legitim_is_bef_n <- is_before %>%
    filter(cs_type_id==1)%>%
    filter(!is.na(quality_issues))%>%
  dplyr::count(issue_string_eng)%>%
  rename(is_before=n)

 legitim_is_n <- is_week1 %>%
   filter(cs_type_id==1)%>%
    filter(!is.na(quality_issues))%>%
  dplyr::count(issue_string_eng)%>%
  rename(is_after=n)

 legitim_right_bef_n <-  right_before %>%
   filter(cs_type_id==1)%>%
    filter(!is.na(quality_issues))%>%
  dplyr::count(issue_string_eng)%>%
  rename(right_before=n)
 
  legitim_right_n <- right_1week %>%
    filter(cs_type_id==1)%>%
    filter(!is.na(quality_issues))%>%
  dplyr::count(issue_string_eng)%>%
  rename(right_after=n)
 
  
m1 <- full_join(legitim_is_bef_n,  legitim_is_n,  by="issue_string_eng")
   
   
m2 <- full_join(legitim_right_bef_n,  legitim_right_n , by="issue_string_eng")

legitim_shift_observations <- full_join(m1,m2 , by="issue_string_eng")


#export excel
#write_xlsx(legitim_shift_observations, "legitim_shift_observations.xlsx")

   
```







**Table B13 Part II: Legitimacy shift of actors***

```{r legitimacy actors numbers of observations}

# add number of core-sentences (N) to legitimacy table 

test <- is_before %>%
    filter(cs_type_id==2)
  
  legitim_actor_is_bef_n<- is_before %>%
    filter(cs_type_id==2)%>%
        filter(!is.na(quality_id))%>%
            filter(!is.na(object1_eng))%>%
      dplyr::count(object1_eng)%>%
  rename(is_before=n)

  
    legitim_actor_is_n<- is_week1 %>%
    filter(cs_type_id==2)%>%
        filter(!is.na(quality_id))%>%
filter(!is.na(object1_eng))%>%
      dplyr::count(object1_eng)%>%
  rename(is_after=n)
    
    legitim_actor_right_before_n <- right_before %>%
    filter(cs_type_id==2)%>%
        filter(!is.na(quality_id))%>%
          filter(!is.na(object1_eng))%>%
      dplyr::count(object1_eng)%>%
  rename(right_before=n)
    
    legitim_actor_right_n <- right_1week %>%
    filter(cs_type_id==2)%>%
        filter(!is.na(quality_id))%>%
filter(!is.na(object1_eng))%>%
      dplyr::count(object1_eng)%>%
  rename(right_after=n)

  
m3 <- full_join(legitim_actor_is_bef_n,  legitim_actor_is_n,  by="object1_eng")
   
   
m4 <- full_join(legitim_actor_right_before_n,legitim_actor_right_n , by="object1_eng")

legitim_shift_actor_observations <- full_join(m3,m4, by="object1_eng")


#export excel
#write_xlsx(legitim_shift_actor_observations, "legitim_shift_actor_observations.xlsx")


```



### Figure C3.  Legitimacy shift of issues (change of average position from one week before to 1 weeks after each attack

```{r plot legitimacy issues per event}


# choose only actor-issue-sentence (cs_type_id==1) 

all_agg <- all %>% 
  group_by(case, period, issue_string_eng, ideology) %>% 
  dplyr::filter(cs_type_id==1) %>%
  dplyr::filter(!is.na(issue_string_eng)) %>% 
  dplyr::filter(!is.na(quality_issues)) %>% 
  dplyr::summarize(mean_leg=mean(quality_issues)) %>% 
  dplyr::filter(case!="other")

all_agg_wide <- spread(all_agg, period, mean_leg)


# plot legitimacy of issues per event

legitimacy_event <- ggplot(all_agg_wide) +
  geom_point(aes(x=as.numeric(before), # values before
                 y=as.numeric(week1), # values after
                 color=as.factor(ideology), # islam/extrism
                 shape = as.factor(issue_string_eng))) +
  geom_label_repel(aes(x=as.numeric(before), 
                       y=as.numeric(week1),
                       label = case),
                  size = 3,
                  label.size = NA, fill = "white") + # more than 6 discrete values -> specify manually. would use it for objects then
  labs(colour = "ideology")+
  labs(shape="issues")+
   ylab("average quality statement after event")+
     xlab("average quality statement before event")+
  geom_abline(intercept = 0, slope = 1, size = 1) + # 45 degree line
  theme_light()


legitimacy_event

#ggsave(filename="legitimacy_event.jpeg", plot=legitimacy_event, device="jpeg", units="cm", width=20, height=13, dpi=300)


```





# Network analysis data preparation

**I used the open-source and free software Gephi for the network analyses. The network analysis is based on the force-directed Fruchterman Reingold algorithm (Fruchterman & Reingold 1991)**


```{r actor issue network right}

## Discourse network after extreme right attacks

## filter edges and nodes for network analysis
##filter minimum of 9 core-sentences per actor
## subject -issue relations - filter n >9 - round for network analysis to positive / negative values

## quality_new indicates "true" positions independent of positive or negative connotation of the subissue group level

## issue network after extreme right attacks; right= 2 weeks after national extreme right attacks

network_right <- right %>% 
    filter(cs_type_id==1)%>% 
  group_by(subject1_id,subissue_group_id,subject1_eng,subissue_group_eng)%>% 
     dplyr::mutate(indicator=case_when(cs_type_string=="Akteur-Themen-Satz" ~ 1)) %>%
  dplyr::mutate(n=sum(indicator,na.rm=TRUE)) %>%
 dplyr:: mutate(average_dir=round(mean(quality_new,na.rm=TRUE))) %>%
 dplyr::select(subject1_id,subissue_group_id,n,average_dir) %>%
unique() %>%
 dplyr::filter(!is.na(subject1_id)) %>%
dplyr::filter(!is.na(subissue_group_id)) %>%
  dplyr::arrange(desc(n)) %>%
  dplyr::filter(n>9)


## filter edges
edges_right <- network_right%>% ungroup() %>% select(-subject1_eng,-subissue_group_eng)

#define target & source of edges
edges_right <-dplyr::rename(edges_right,source=subject1_id)

edges_right <-dplyr::rename(edges_right,target=subissue_group_id)

edges_right <-dplyr::rename(edges_right,weight=n)

# export dataframes as csv/excel

#write_delim(edges_right, delim = ",", file="edges_right.csv")



##filter nodes -actors
nodes_actor_right<- network_right %>% ungroup() %>% select(subject1_eng,subject1_id)%>% unique()

#define id & label

nodes_actor_right <-dplyr::rename(nodes_actor_right,id=subject1_id)

nodes_actor_right<-dplyr::rename(nodes_actor_right,label=subject1_eng)



## filter nodes - issues

nodes_issue_right <- network_right %>% ungroup() %>% select(subissue_group_eng, subissue_group_id)%>% unique()

#define id & label

nodes_issue_right <-dplyr::rename(nodes_issue_right,id=subissue_group_id)

nodes_issue_right <-dplyr::rename(nodes_issue_right,label=subissue_group_eng)

# merge nodes

nodes_actorissue_right <- bind_rows(nodes_actor_right,nodes_issue_right)

# export dataframes as csv/excel

#write_delim(nodes_actorissue_right, delim = ",", file="nodes_actorissue_right.csv")


```





```{r actor issue network is}

## Discourse network after Islamist attacks

## filter edges and nodes for network analysis

## subject -issue relations - filter n>9 - round for network analysis to positive / negative values


## issue network after Islamist attacks
# is = 2 weeks after national Islamist attacks

network_is <- is %>% 
    filter(cs_type_id==1)%>% 
  group_by(subject1_id,subissue_group_id,subject1_eng,subissue_group_eng)%>% 
     dplyr::mutate(indicator=case_when(cs_type_string=="Akteur-Themen-Satz" ~ 1)) %>%
  dplyr::mutate(n=sum(indicator,na.rm=TRUE)) %>%
 dplyr:: mutate(average_dir=round(mean(quality_new,na.rm=TRUE))) %>%
 dplyr::select(subject1_id,subissue_group_id,n,average_dir) %>%
unique() %>%
 dplyr::filter(!is.na(subject1_id)) %>%
dplyr::filter(!is.na(subissue_group_id)) %>%
  dplyr::arrange(desc(n)) %>%
  dplyr::filter(n>9)


## filter edges
edges_is <- network_is%>% ungroup() %>% select(-subject1_eng,-subissue_group_eng)

#define target & source of edges
edges_is <-dplyr::rename(edges_is,source=subject1_id)

edges_is <-dplyr::rename(edges_is,target=subissue_group_id)

edges_is <-dplyr::rename(edges_is,weight=n)

# export dataframes as csv/excel

#write_delim(edges_is, delim = ",", file="edges_is.csv")


##filter nodes -actors
nodes_actor_is<- network_is %>% ungroup() %>% select(subject1_eng,subject1_id)%>% unique()

#define id & label

nodes_actor_is <-dplyr::rename(nodes_actor_is,id=subject1_id)

nodes_actor_is <-dplyr::rename(nodes_actor_is,label=subject1_eng)


## filter nodes - issues

nodes_issue_is <- network_is %>% ungroup() %>% select(subissue_group_eng, subissue_group_id)%>% unique()

#define id & label

nodes_issue_is <-dplyr::rename(nodes_issue_is,id=subissue_group_id)

nodes_issue_is <-dplyr::rename(nodes_issue_is,label=subissue_group_eng)


# merge nodes

nodes_actorissue_is <- bind_rows(nodes_actor_is,nodes_issue_is)

# export dataframes as csv/excel

#write_delim(nodes_actorissue_is, delim = ",", file="nodes_actorissue_is.csv")



```




```{r node label for all actors and issues}

## select nodes for the issue actor network

## filter actor nodes = actor type &id

node_actor_label <- all_after%>% 
  group_by(subject1_eng,subject1_id)%>% 
   filter(!is.na(subject1_eng)) %>% 
  mutate(count=1) %>% 
  dplyr::summarise(count = sum(count))%>% 
  arrange(subject1_id)%>%
select(-count)


#define id & label

node_actor_label <-dplyr::rename(node_actor_label,id=subject1_id)

node_actor_label <-dplyr::rename(node_actor_label,label=subject1_eng)


## filter issue nodes = subissue groups & id

node_issue_label <- all_after%>% 
  dplyr::group_by(subissue_group_eng,subissue_id)%>% 
   dplyr::filter(!is.na(subissue_id)) %>% 
  unique() %>%
   dplyr::mutate(count=1) %>% 
  dplyr::summarise(count = sum(count))%>% 
  arrange(subissue_id)%>%
select(-count)


#define id & label

node_issue_label <-dplyr::rename(node_issue_label,id=subissue_id)

node_issue_label  <-dplyr::rename(node_issue_label,label=subissue_group_eng)


# merge nodes

node_actorissue<- bind_rows(node_issue_label,node_actor_label)

## export dataframes as csv/excel

#all nodes

#write_delim(node_actorissue, delim = ",", file="node_actorissue.csv")



```





```{r snapshot}


#renv::snapshot()



```







